package com.pangda.common.domain;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.constant.HttpStatusConstant;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageResult<T> {
    @ApiModelProperty(value = "总页数")
    private Integer pageNum;

    @ApiModelProperty(value = "每页条数")
    private Integer pageSize;

    /**
     * 响应返回编码
     */
    @ApiModelProperty(value = "状态码")
    private int code;

    /**
     * 响应返回信息
     */
    @ApiModelProperty(value = "状态信息")
    private String msg;

    @ApiModelProperty(value = "总条目数", required = true)
    private Long total = 0L;

    @ApiModelProperty(value = "数据列表", required = true)
    private List<T> rows;

    @ApiModelProperty("参数")
    private Map<String, Object> params;


    public static <T> PageResult<T> success(Page<?> page, Class<T> clazz) {
        return PageResult.<T>builder()
                .code(HttpStatusConstant.SUCCESS)
                .msg("查询成功")
                .total(page.getTotal())
                .rows(BeanUtil.copyToList(page.getRecords(), clazz))
                .build();
    }

    public static <T> PageResult<T> success(List list, Class<T> clazz) {
        return PageResult.<T>builder()
                .code(HttpStatusConstant.SUCCESS)
                .msg("查询成功")
                .rows(BeanUtil.copyToList(list, clazz))
                .build();
    }


}
